#include "bacterialcontigbrowser.h"


#include "contigbrowsermenubar.h"

ContigBrowserMenuBar::ContigBrowserMenuBar(BacterialContigBrowser *parent) :
    QGraphicsWidget(parent)
{
    width = parent->boundingRect().width()*.05;
    height = parent->boundingRect().height();

    qDebug() << "creating menu bar " << endl;
    layout = new QGraphicsLinearLayout;
    layout->setOrientation(Qt::Vertical);
    layout->setMaximumWidth(width);
    setLayout(layout);

    //PROGRESS LABEL
    progressLabel = new QLabel;
    progressLabel->setText("");

    progressLabelProxy = new QGraphicsProxyWidget(this);
    progressLabelProxy->setWidget( progressLabel );

    layout->addItem( progressLabelProxy );

    //PROGRESS BAR
    progressBar = new QProgressBar;
    progressBar->setMinimum(0);
    progressBar->setMaximum(100);
    progressBar->setValue(0);
    progressBar->setDisabled(true);

    progressBarProxy = new QGraphicsProxyWidget(this);
    progressBarProxy->setWidget(progressBar);

    layout->addItem( progressBarProxy );

    //EXPORT BUTTON
    exportButton = new QPushButton;
    exportButton->setText("Export");
    exportButton->setMaximumWidth(width*.95);
    exportProxyButton = new QGraphicsProxyWidget(this);
    exportProxyButton->setWidget(exportButton);

    layout->addItem(exportProxyButton);

    connect( exportButton, SIGNAL(clicked()), parent, SLOT(exportSelectedGenesSlot()) );

    //COMBO BOX
    sortByComboBox = new QComboBox;
    sortByComboBox->insertItem(0, "contig length");
    sortByComboBox->insertItem(1, "selected genes");
    sortByComboBoxProxy = new QGraphicsProxyWidget(this);
    sortByComboBoxProxy->setWidget(sortByComboBox);
    sortByComboBoxProxy->setZValue(100);

    layout->addItem(sortByComboBoxProxy);

    //SORT BUTTON
    sortButton = new QPushButton;
    sortButton->setText("Sort");
    sortButtonProxy = new QGraphicsProxyWidget(this);
    sortButtonProxy->setWidget(sortButton);

    layout->addItem(sortButtonProxy);

    connect( sortButton, SIGNAL(clicked()), this, SLOT( sortButtonClickedSlot()));
    connect( this, SIGNAL(sortSignal(int)), parent, SLOT(sortSlot(int)));

    //LINES -- doesn't work
//    lines << new QGraphicsLineItem( 0, 0, boundingRect().width(), 0 );
//    lineLayoutItems << new QGraphicsLayoutItem(this);
//    lines[0]->setParentItem(lineLayoutItems[0]);
//    //lineProxys << new QGraphicsProxyWidget(this);
//    //lineProxys[0]->setWidget( lines[0] );

//    layout->addItem( lineLayoutItems[0]);
}

void ContigBrowserMenuBar::sortButtonClickedSlot()
{
    emit( sortSignal(sortByComboBox->currentIndex()) );
}

//----------------------------SETTERS-------------------------------

void ContigBrowserMenuBar::setWidth(float newWidth)
{
    width = newWidth;
}

void ContigBrowserMenuBar::setHeight(float newHeight)
{
    height = newHeight;
}

void ContigBrowserMenuBar::updateProgressBar(int steps)
{
    if( !progressBar->isEnabled() )
    {
        progressBar->setEnabled(true);
    }
    progressBar->setValue(steps);

    update();
}

void ContigBrowserMenuBar::updateProgressMessage(QString message)
{
    progressLabel->setText(message);

    update();
}

void ContigBrowserMenuBar::resetProgressBar()
{
    progressBar->setValue(0);
    progressBar->setDisabled(true);
}

//------------------------END SETTERS-------------------------------
//-----------------------------DRAW FUNCTIONS-----------------------
QRectF ContigBrowserMenuBar::boundingRect() const
{
    return QRectF( 0, 0, width, height);
}

QPainterPath ContigBrowserMenuBar::shape() const
{
    QPainterPath path;
    path.addRect(boundingRect());
    return path;
}


void ContigBrowserMenuBar::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    QColor backgroundColor;
    backgroundColor.setRgb(60.0, 60.0, 60.0, 255.0);
    painter->setBrush(backgroundColor);
    painter->setPen( Qt::NoPen );
    QRectF bRect = boundingRect();
    bRect.adjust(0, 1, 0, -1);
    painter->drawRect(bRect);

}
//------------------------------END DRAW FUNCTIONS--------------------------------
